import { param2str, str2param } from './url'

import pagesData from '@/pages.json'

const pages = pagesData['pages']

const subPackages = pagesData?.['subPackages'] || []

// #ifndef H5
const tabBar = pagesData?.['tabBar'] || []
// #endif

// 获取页面信息
export const getPagesConfig = (path = '') => {
  path = path || getPagePath() + '?'
  const pathName = path.slice(0, path.indexOf('?'))
  const index = path.indexOf('/', 1)
  const PackageName = path.slice(path[0] == '/' ? 1 : 0, index) //区分主包或分包
  path = pathName.slice(index)
  if (PackageName !== 'pages') {
    const pageIndex = subPackages.find((v) => v.root === PackageName)
    return pageIndex.pages.find((v) => path.indexOf(v.path) != -1) || {}
  } else {
    return pages.find((v) => v.path.indexOf(path) != -1) || {}
  }
}

// 获取配置的页面标题
export const getPageText = () => {
  let title = getPagesConfig()
  return title?.style?.navigationBarTitleText || ''
}

// 判断当前页或者传入的页面是否是tab页面
export const isTabBarPage = (path = '') => {
  // #ifdef H5
  return false
  // #endif
  if (!tabBar?.list) return false
  let pagePath = path || getPagePath()
  return tabBar.list.some((v) => pagePath.indexOf(v.pagePath) != -1)
}

const _getAccountInfoSync = uni.getAccountInfoSync

export const getAccountInfoSync = function () {
  // eslint-disable-next-line no-unreachable
  if (_getAccountInfoSync) {
    return _getAccountInfoSync()
  } else {
    return {}
  }
}

export const systemInfo = uni.getSystemInfoSync()

// 顶部导航高度
export const getNavbarHeight = function (height) {
  let { statusBarHeight } = systemInfo
  /* #ifdef MP-TOUTIAO */
  // 布局权限未下来
  // return 0
  /* #endif */

  // #ifdef APP-PLUS || H5
  return (height || 44) + statusBarHeight
  // #endif
  // #ifdef MP
  // 小程序特别处理，让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
  // 此方法有缺陷，暂不用(会导致少了几个px)，采用直接固定值的方式
  // return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
  // eslint-disable-next-line no-unreachable
  let h = height || systemInfo.platform == 'ios' ? 44 : 44

  return h + statusBarHeight
  // #endif
}

// 获取底部导航高度
export const getTabbarHeight = function (midButton = false, height = 50) {
  return systemInfo.safeAreaInsets.bottom + height + (midButton ? 48 : 0)
}

// 保证页面加载完毕
export const checkMountedPages = function () {
  return new Promise((resolve) => {
    let pages = getCurrentPages()

    if (pages.length) {
      resolve(pages)
    } else {
      setTimeout(() => {
        checkMountedPages().then((ret) => {
          resolve(ret)
        })
      }, 30)
    }
  })
}

// 获取当前路由
export const getCurPageAsync = async function () {
  let pages = await checkMountedPages()
  return pages[pages.length - 1]
}

// 获取当前路由
export const getCurPage = function () {
  let pages = getCurrentPages()
  let page = pages[pages.length - 1]
  return page
}

// 获取当前页面路径
export const getPagePath = (path = true) => {
  let page = getCurPage()
  if (!page) {
    page = getAppLaunchInfo()
  }
  return path ? '/' + page.route : page
}

// 获取启动参数
export const getAppLaunchInfo = function (appLaunchInfo) {
  if (uni.$appLaunchInfo) {
    return uni.$appLaunchInfo
  } else if (!appLaunchInfo && uni.getLaunchOptionsSync) {
    appLaunchInfo = uni.getLaunchOptionsSync()
  }
  if (appLaunchInfo) {
    let { query, path } = appLaunchInfo
    let obj = {
      options: query,
      route: path,
      $page: {
        fullPath: path + param2str(query)
      }
    }
    uni.$appLaunchInfo = { ...obj }
    return obj
  }
  return {}
}

// 获取当前路由 参数
export const getCurPageQuery = function () {
  let page = getPagePath(false)
  if (!page) {
    page = getAppLaunchInfo()
  }
  let query = page.$page.options || {}
  let { scene } = query
  if (scene) {
    query = {
      ...query,
      ...(str2param(decodeURIComponent(decodeURIComponent(scene))) || {})
    }
  }
  return query || {}
}
